class TimestampUtils{
  /// 从JSON值转换为DateTime对象
  static DateTime? fromJson(dynamic value){
    if(value ==null) return null;
    try{
      if(value is int){
        // 假设时间戳单位为秒（如果是毫秒，则不需要乘以1000）
        return DateTime.fromMillisecondsSinceEpoch(value * 1000);
      }else if(value is String){
        return DateTime.tryParse(value);
      }
      // else if(value is Double){
      //   return DateTime.fromMillisecondsSinceEpoch((value * 1000).toInt());
      // }
    }catch(e){
      print('时间戳转换错误: $e');
    }
    return null;
  }

  /// 将DateTime对象转换为JSON值（秒级时间戳）
  static dynamic toJson(DateTime? date) {
    if (date == null) return null;
    return date.millisecondsSinceEpoch ~/ 1000;
  }

  /// 从毫秒时间戳转换为DateTime对象
  static DateTime? fromMillis(dynamic value) {
    if (value == null) return null;

    try {
      if (value is int) {
        return DateTime.fromMillisecondsSinceEpoch(value);
      } else if (value is String) {
        return DateTime.tryParse(value);
      } else if (value is double) {
        return DateTime.fromMillisecondsSinceEpoch(value.toInt());
      }
    } catch (e) {
      print('毫秒时间戳转换错误: $e');
    }

    return null;
  }

  /// 将DateTime对象转换为毫秒时间戳
  static int? toMillis(DateTime? date) {
    if (date == null) return null;
    return date.millisecondsSinceEpoch;
  }

  /// 格式化日期时间为字符串
  static String format(DateTime? date, {String pattern = 'yyyy-MM-dd HH:mm'}) {
    if (date == null) return '';

    final year = date.year.toString().padLeft(4, '0');
    final month = date.month.toString().padLeft(2, '0');
    final day = date.day.toString().padLeft(2, '0');
    final hour = date.hour.toString().padLeft(2, '0');
    final minute = date.minute.toString().padLeft(2, '0');

    return pattern
        .replaceAll('yyyy', year)
        .replaceAll('MM', month)
        .replaceAll('dd', day)
        .replaceAll('HH', hour)
        .replaceAll('mm', minute);
  }

  /// 计算相对时间（如 "2分钟前"）
  static String relativeTime(DateTime? date) {
    if (date == null) return '';

    final now = DateTime.now();
    final difference = now.difference(date);

    if (difference.inSeconds < 60) {
      return '刚刚';
    } else if (difference.inMinutes < 60) {
      return '${difference.inMinutes}分钟前';
    } else if (difference.inHours < 24) {
      return '${difference.inHours}小时前';
    } else if (difference.inDays < 30) {
      return '${difference.inDays}天前';
    } else {
      return format(date);
    }
  }
  /// 判断日期是否是今天
  static bool isToday(DateTime? date) {
    if (date == null) return false;
    final now = DateTime.now();
    return date.year == now.year &&
        date.month == now.month &&
        date.day == now.day;
  }
  /// 判断日期是否是本周
  static bool isThisWeek(DateTime? date) {
    if (date == null) return false;
    final now = DateTime.now();
    final startOfWeek = now.subtract(Duration(days: now.weekday - 1));
    return date.isAfter(startOfWeek) && date.isBefore(now.add(Duration(days: 1)));
  }

  /// 获取日期（不含时间）
  static DateTime dateOnly(DateTime date) {
    return DateTime(date.year, date.month, date.day);
  }


  /// 添加工作日
  static DateTime addBusinessDays(DateTime date, int days) {
    DateTime result = date;
    int added = 0;

    while (added < days) {
      result = result.add(const Duration(days: 1));
      if (result.weekday != DateTime.saturday && result.weekday != DateTime.sunday) {
        added++;
      }
    }

    return result;
  }

  /// 计算两个日期之间的工作日
  static int businessDaysBetween(DateTime start, DateTime end) {
    int days = 0;
    DateTime current = start;

    while (current.isBefore(end)) {
      if (current.weekday != DateTime.saturday && current.weekday != DateTime.sunday) {
        days++;
      }
      current = current.add(const Duration(days: 1));
    }

    return days;
  }
}

